<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">{{pageTitle}}</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<view style="width: 100%;">
				<uni-forms ref="baseForm" :modelValue="baseFormData">
					<uni-forms-item label="工作组名称：" labelWidth="100" labelAlign="right" required>
						<uni-easyinput v-model="baseFormData.name" placeholder="请填写" />
					</uni-forms-item>
					<uni-forms-item label="工作组成员：" labelWidth="100" labelAlign="right" required>
						<view class="item-back law-item-back">
							<input @input="onKeyInput" class="selectLaw" @focus="startEditAction"
								v-model="baseFormData.code" placeholder="输入律师姓名/手机号"></input>
							<view v-if="showMask" class="selectLaw-list">
								<scroll-view class="pop-content-back" scroll-y="true">
									<view class="pop-content-list" v-for="(item,index) in lawyerArr" :key="index"
										@click="clickPopIremAction(item)">
										<image class="pop-content-img" :src="item.account.avatarPic"></image>
										<view class="pop-content-name">{{item.account.realName}}</view>
										<view class="pop-content-phone">{{item.account.mobile}}</view>
									</view>
								</scroll-view>
							</view>
						</view>
						<view class="mask-view" v-if="showMask" @tap="tapMask"></view>
						<view class="customer-item-back">
							<view class="customer-item" v-for="(item,index) in selectedArr" :key="index">
								<view class="customer-back">
									<image class="customer-img" :src="item.account.avatarPic"></image>
									<view class="customer-name">{{item.account.realName}}</view>
									<view class="customer-phone">{{item.account.mobile}}</view>
									<image class="customer-close" @click="clickDelAction(index)"
										src="../../../static/phone/guanbi1.png"></image>
								</view>
							</view>
						</view>
					</uni-forms-item>

				</uni-forms>
			</view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
				<view class="bottom-btn" @click="requestSaveData">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	let app = ''
	
	export default {
		props: {
			selectItem: {
				type: Object,
				default: {}
			},
			editType: {
				type: String,
				default: 'add',
			},
		},
		data() {
			return {
				role: '', //当前登录角色
				roleUrl: '',
				pageTitle: '新增',
				showMask: false,
				
				baseFormData: {
					'name': '',
					'code': '',
					'id': ''
				},
				tableList: [], //所有数据
				lawyerArr: [], //去掉已选
				selectedArr: [], //已选择
			}
		},
		mounted() {
			var _this = this;
			app = getApp()
			_this.role = app.globalData.role
			_this.roleUrl = _this.$roleUrl({
				tab: _this.role
			})
			if (this.editType == 'edit') {
				this.pageTitle = '编辑';
				this.baseFormData.name = this.selectItem.name;
				this.selectedArr = this.selectItem.users;
				this.baseFormData.id = this.selectItem.id;
			}
			this.requestData();
		},

		methods: {
			//网络请求示例
			async requestData() {
				let _this = this
				
				var url = "/admin/lawyer/list";
				let parmas = {};
				if (_this.baseFormData.code) {
					this.$set(parmas, 'keyword', _this.baseFormData.code)
				}
				const res = await this.$myRequest({
					url: url,
					method: 'POST', //默认是GET
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.tableList = res.data.data.list;
					_this.refreshDataAction();
				}
			},
			//保存
			async requestSaveData() {
				if (!this.baseFormData.name) {
					uni.showToast({
						title: '请填写工作组名称',
						icon: 'none'
					})
					return;
				}
				if (!this.selectedArr) {
					uni.showToast({
						title: '请添加工作组成员',
						icon: 'none'
					})
					return;
				}
				let _this = this;
				var arr = [];
				for (var i = 0; i < this.selectedArr.length; i++) {
					arr.push(this.selectedArr[i].id)
				}
				console.log(_this.editType,this.baseFormData.id)
				let parmas = {
					'userIds': arr,
					'name': this.baseFormData.name,
					'id': _this.editType == 'edit' ? this.baseFormData.id : ''
				}
				console.log(parmas)
				const res = await this.$myRequest({
					url: `/${_this.roleUrl}/workGroup`,
					method: _this.editType == 'edit' ? 'PUT' : 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					uni.showToast({
						title: _this.editType == 'edit' ? '编辑成功' : '添加成功'
					})
					_this.$emit("successBack");
					_this.closeBtnAction()
				}
			},

			startEditAction(e) {
				console.log("开始编写")
				console.log(e)
				this.showMask = true
			},
			onKeyInput(event) {
				console.log(event.target.value)
				this.requestData()
			},
			tapMask() {
				this.showMask = false
			},
			
			//选择工作组、律师
			clickPopIremAction(item) {
				this.showMask = false;
				this.selectedArr.push(item);
				this.refreshDataAction();
			},

			//刷新律师列表
			refreshDataAction() {
				this.lawyerArr = [];
				for (var i = 0; i < this.tableList.length; i++) {
					var value = this.tableList[i];
					var hasSelect = false;
					for (var j = 0; j < this.selectedArr.length; j++) {
						var tem = this.selectedArr[j];
						if (value.account.id == tem.account.id) {
							hasSelect = true;
						}
					}
					if (!hasSelect) {
						this.lawyerArr.push(value);
					}
				}
			},
			//删除律师
			clickDelAction(index) {
				this.selectedArr.splice(index, 1);
				this.refreshDataAction();
			},
			closeBtnAction() {
				this.$emit('addTeamClose')
			},
			moveHandle() {
				return true
			},
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 199;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 100;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 552px;
		height: 500px;
		padding: 24px;
		max-height: 500px;
		background-color: white;
		margin-top: -50px;
		border-radius: 4px;
		z-index: 101;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;

	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {

		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}

	.item-back {
		width: 350px
	}

	.customer-item {
		display: flex;
		margin-top: 8px;
		align-items: center;
	}

	.customer-back {
		min-width: 168px;
		height: 44px;
		background: #E8F7FB;
		border-radius: 4px;
		display: flex;
		align-items: center;
		margin-right: 10px;
	}

	.customer-img {
		width: 20px;
		height: 20px;
		border-radius: 50%;
		margin-left: 8px;
	}

	.customer-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 2px;
	}

	.customer-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 4px;
	}

	.customer-close {
		width: 16px;
		height: 16px;
		margin-right: 2px;
		margin-left: auto;
	}

	.customer-item-back {
		display: flex;
		flex-wrap: wrap;
	}

	.selectLaw {
		font-size: 14px;
		border: 1px solid #DCDFE6;
		border-radius: 4px;
		padding: 6px 10px;
		position: relative;

		flex-direction: row;
		align-items: center;
		z-index: 100000;
		background-color: white;
	}

	.law-item-back {
		background-color: white;
	}

	.selectLaw-list {
		margin-top: 2px;
		position: absolute;
		width: 350px;
		height: 180px;
		z-index: 100010;
		background-color: white;
		box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.2000);
		border-radius: 2px;
	}

	.mask-view {
		position: absolute;
		width: 100vw;
		height: 100vh;
		z-index: 9999;
		background-color: rgba(0, 0, 0, 0.0);
		top: -100px;
		left: 0;
		right: 0;
		bottom: 0;
	}

	.pop-content-back {
		margin-left: 20px;
		width: 328px;
		height: 100%;
	}

	.pop-content-list {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
		margin-top: 10px;
	}

	.pop-content-img {
		width: 20px;
		min-width: 20px;
		height: 20px;
		border-radius: 50%;
	}

	.pop-content-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 6px;
	}

	.pop-content-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 10px;
	}
</style>
